/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.max.oti.system.bean.sgt;

import com.max.oti.system.model.Modality;
import com.max.oti.system.model.Requirement;
import com.max.oti.system.model.TypeComission;
import com.max.oti.system.service.interfac.IModalityService;
import com.max.oti.system.service.interfac.IRequirementService;
import com.max.oti.system.service.interfac.ITypeComissionService;
import com.max.oti.system.util.FacesUtil;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import org.primefaces.event.RowEditEvent;

@ManagedBean
@ViewScoped
public class RequirementBean implements Serializable {

    Requirement requirement = new Requirement();
    @ManagedProperty("#{requirementService}")
    IRequirementService requirementService;
    //para la modalidad
    @ManagedProperty("#{modalityService}")
    IModalityService modalityService;
    @ManagedProperty("#{typeComissionService}")
    ITypeComissionService typeComissionService;
    Modality modality = new Modality();
    List<Modality> listModality;
    List<Modality> listModalityRequeriment;

    //para la comsion
    @PostConstruct
    public void initCollection() {
        try {
//            requirement.setIdTypeComission(new TypeComission(2));
//            listRequirement = requirementService.listHQL("FROM Requirement r");
            listModality = modalityService.listHQL("FROM Modality o");
            listModalityRequeriment = modalityService.listHQL("FROM Modality m", 1);
            //System.out.println(listModality.size());
        } catch (Exception ex) {
            Logger.getLogger(RequirementBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void onEdit(RowEditEvent event) {
        Requirement requ = (Requirement)event.getObject();
        try {
            requirementService.update(requ);
            FacesUtil.addMessage("Actualizado...", null, FacesMessage.SEVERITY_INFO);
            //requirementService.HQLexecute("update Requirement r "
            //        + "set r.name='" + requ.getName() + "' where r.id=" + requ.getId());
        } catch (Exception ex) {
            Logger.getLogger(RequirementBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void saveRequirement() {

        if (requirement.getId() == null) {
            try {
                TypeComission temoTypeCom = (TypeComission) typeComissionService.
                        listHQL("select t.id as id, t.code as code, t.name as name from TypeComission t where t.code='sgt'",
                        TypeComission.class).get(0);
                requirement.setNumber(0);
                requirement.setIdTypeComission(temoTypeCom);
                requirementService.save(requirement);
                listModalityRequeriment = modalityService.listHQL("FROM Modality m", 1);

                requirement = new Requirement();
                FacesUtil.addMessage("Registro con Exito", null, FacesMessage.SEVERITY_INFO);
            } catch (Exception e) {
                FacesUtil.addMessage("Error al Registrar", null, FacesMessage.SEVERITY_FATAL);
            }
        } else {
            try {
                requirementService.update(requirement);
                FacesUtil.addMessage("Modificacion con Exito", null, FacesMessage.SEVERITY_INFO);
                requirement = new Requirement();
                listModalityRequeriment = modalityService.listHQL("FROM Modality m", 1);
            } catch (Exception ex) {
                FacesUtil.addMessage("Error en la Modificacion", null, FacesMessage.SEVERITY_INFO);
            }
        }
    }

    //<editor-fold defaultstate="collapsed" desc=" METODOS GETTER Y SETTER ">
    public Requirement getRequirement() {
        return requirement;
    }

    public void setRequirement(Requirement requirement) {
        this.requirement = requirement;
    }

    public List<Modality> getListModalityRequeriment() {
        return listModalityRequeriment;
    }

    public void setListModalityRequeriment(List<Modality> listModalityRequeriment) {
        this.listModalityRequeriment = listModalityRequeriment;
    }

    public IRequirementService getRequirementService() {
        return requirementService;
    }

    public void setRequirementService(IRequirementService requirementService) {
        this.requirementService = requirementService;
    }

    //para la modaliad
    public Modality getModality() {
        return modality;
    }

    public void setModality(Modality modality) {
        this.modality = modality;
    }

    public List<Modality> getListModality() {
        return listModality;
    }

    public void setListMFodality(List<Modality> listModality) {
        this.listModality = listModality;
    }

    public void setTypeComissionService(ITypeComissionService typeComissionService) {
        this.typeComissionService = typeComissionService;
    }

    public IModalityService getModalityService() {
        return modalityService;
    }

    public void setModalityService(IModalityService modalityService) {
        this.modalityService = modalityService;
    }
    //</editor-fold> 
}
